package com.itheima.web.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.CommentVo;
import com.itheima.domain.vo.MovementVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.VideoVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.service.mongo.VideoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class UserManager {
    @DubboReference
    private UserInfoService userInfoService;


    public ResponseEntity findUsersByPage(Integer page, Integer pagesize) {
        PageBeanVo pageBeanVo = userInfoService.findByPage(page, pagesize);

        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findUserInfoById(Long userId) {
        UserInfo userInfo = userInfoService.findByUserId(userId);
        return ResponseEntity.ok(userInfo);
    }

    @DubboReference
    private MovementService movementService;

    public ResponseEntity findMessages(Long uid, Integer state, Integer page, Integer pagesize) {
        PageBeanVo pageBeanVo = movementService.findMyMovementByPage(uid, state, page, pagesize);
        List<Movement> movementList = (List<Movement>) pageBeanVo.getItems();
        ArrayList<MovementVo> voList = new ArrayList<>();
        for (Movement movement : movementList) {
            UserInfo userInfo = userInfoService.findByUserId(movement.getUserId());
            MovementVo movementVo = new MovementVo();
            movementVo.setUserInfo(userInfo);
            movementVo.setMovement(movement);
            String dateTime = DateUtil.formatDateTime(new Date(movement.getCreated()));
            movementVo.setCreateDate(dateTime);
            voList.add(movementVo);
        }
        pageBeanVo.setItems(voList);

        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findMovementVoById(String publishId) {
        Movement movement = movementService.findById(new ObjectId(publishId));
        UserInfo userInfo = userInfoService.findByUserId(movement.getUserId());
        MovementVo movementVo = new MovementVo();
        movementVo.setUserInfo(userInfo);
        movementVo.setMovement(movement);
        String dateTime = DateUtil.formatDateTime(new Date(movement.getCreated()));
        movementVo.setCreateDate(dateTime);
        return ResponseEntity.ok(movementVo);
    }

    @DubboReference
    private CommentService commentService;

    public ResponseEntity findCommentById(Integer page, Integer pagesize, String messageID) {
        PageBeanVo pageBeanVo = commentService.findCommentByPage(new ObjectId(messageID), 2, page, pagesize);
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();
        ArrayList<CommentVo> voList = new ArrayList<>();
        for (Comment comment : commentList) {
            UserInfo userInfo = userInfoService.findByUserId(comment.getUserId());
            CommentVo vo = new CommentVo();

            vo.setId(comment.getId().toHexString());
            vo.setNickname(userInfo.getNickname());
            vo.setUserId(comment.getUserId());
            vo.setContent(comment.getContent());
            vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated())));
            voList.add(vo);
            System.out.println(vo);

        }
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }

    @DubboReference
    private VideoService videoService;

    public ResponseEntity findVideoById(Integer page, Integer pagesize, Long userId) {
        PageBeanVo pageBeanVo = videoService.findVideoByPage(userId, page, pagesize);
        List<Video> videoList = (List<Video>) pageBeanVo.getItems();
        List<VideoVo> voList = new ArrayList<>();
        for (Video video : videoList) {
            VideoVo vo = new VideoVo();
            UserInfo userInfo = userInfoService.findByUserId(video.getUserId());
            vo.setVideo(video);
            vo.setUserInfo(userInfo);
            voList.add(vo);
        }
        pageBeanVo.setItems(voList);

        return ResponseEntity.ok(pageBeanVo);
    }
}
